﻿using AutoMapper;
using HelenServer.UserCenter.Contracts;

namespace HelenServer.UserCenter.Dal;

[ServiceProvider(typeof(IDalUserService), SqlServerConstants.ProviderName)]
public class DalUserService : IDalUserService
{
    private readonly IRepo<UserCenterDbContext> _repo;
    private readonly IGrpcClientFactory _factory;
    private readonly IMapper _mapper;

    public DalUserService(UserCenterDbContext context, IGrpcClientFactory factory, IMapper mapper)
    {
        _repo = new Repo<UserCenterDbContext>(context);
        _factory = factory;
        _mapper = mapper;
    }

    public async Task<OperationResult<int>> AddAsync(Operation<UserCreateModel> operation, CancellationToken cancellationToken = default)
    {
        var entity = _mapper.Map<UserCreateModel, User>(operation.Parameter);

        if (await _repo.FindAsync<User>(n => n.Id == entity.Id, cancellationToken: cancellationToken) is not null)
        {
            return OperationResult<int>.Failed("用户已存在");
        }

        if (entity.Password.Length < 6)
        {
            return OperationResult<int>.Failed("密码过短");
        }

        entity.Password = Helen.Encrypt(entity.Password);

        //if (avatar is not null)
        //{
        //    using var memoryStream = new MemoryStream();
        //    await avatar.CopyToAsync(memoryStream, cancellationToken);

        //    var fs = _factory.GetService<IFSUploadFile, UploadGrpcModel, GrpcResult<string>>("fs");

        //    var fsModel = new UploadGrpcModel
        //    {
        //        GroupName = "group1",
        //        ClusterName = "",
        //        Name = "",
        //        FileExt = "",
        //        Buffer = memoryStream.ToArray()
        //    };

        //    var grpcResult = await fs.InvokeAsync(fsModel, new Metadata(), cancellationToken);

        //    entity.Avatar = grpcResult.Data;
        //}

        var result = await _repo.AddAsync(entity, cancellationToken);

        return OperationResult<int>.OK(result);
    }

    public async Task<OperationResult<int>> AddRangeAsync(Operation<IEnumerable<UserCreateModel>> operation, CancellationToken cancellationToken = default)
    {
        var entities = _mapper.Map<IEnumerable<UserCreateModel>, IEnumerable<User>>(operation.Parameter);

        var result = await _repo.AddRangeAsync(entities, cancellationToken);

        return result > 0 ? OperationResult<int>.OK(result) : OperationResult<int>.Failed("批量新增用户失败");
    }

    public async Task<OperationResult<int>> AddIdentityRangeAsync(Operation<IEnumerable<UserCreateModel>> operation, CancellationToken cancellationToken = default)
    {
        var entities = _mapper.Map<IEnumerable<UserCreateModel>, IEnumerable<User>>(operation.Parameter);

        var result = await _repo.AddIdentityRangeAsync(entities, cancellationToken);

        return result > 0 ? OperationResult<int>.OK(result) : OperationResult<int>.Failed("批量新增用户失败");
    }

    public Task<bool> DeleteAsync(Operation<string> operation, CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<bool> ExistsAsync(Operation<string> operation, CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<UserInfoModel> GetAsync(Operation<string> operation, CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<IReadOnlyCollection<UserInfoModel>> GetListAsync(Operation<UserSearchModel> operation, CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<bool> UpdateAsync(Operation<UserModel> operation, CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<bool> UpdatePasswordAsync(Operation<UserPasswordModel> operation, CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<bool> UpdateStatusAsync(Operation<UserStatusModel> operation, CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }
}